package org.libra {
	import flash.events.Event;
	import flash.events.MouseEvent;
	import org.libra.basic.Container;
	import org.libra.events.LibraEvent;
	import org.libra.geom.IntDimension;
	import org.libra.geom.IntPoint;
	import org.libra.managers.LibraManager;
	import org.libra.ui.interfaces.IButtonState;
	
	/**
	 * ...
	 * @author zhangzuoqiang
	 */
	public class JPanel extends JWindow {
		
		public static const CLICKED:String = "clicked";
		private var defaultButton:JButton;
		private var modal:Boolean;
		private var closeBtn:JButton;
		private var closeable:Boolean;
		
		public function JPanel(owner:Container, modal:Boolean = false) { 
			super();
			this.owner = owner;
			this.modal = modal;
			this.setMasked(true);
			this.closeBtn = new JButton("关闭");
			this.closeBtn.pack();
			this.append(closeBtn);
			closeable = true;
		}
		
		override public function show():void {
			if (!showing) {
				this.showing = this.owner.append(this);
				if (this.showing) {
					this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
					LibraManager.getInstance().addPanel(this);
					if (!isDrawedMe()) {
						drawMe();
					}
				}
				if (closeable) {
					this.closeBtn.addActionListener(onCloseHandler);
				}
			}
		}
		
		override public function tryToClose():void {
			if (showing) {
				if (this.owner.remove(this, false)) { 
					this.showing = false;
					this.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
					LibraManager.getInstance().removePanel(this);
				}
				if (closeable) {
					this.closeBtn.removeActionListener(onCloseHandler);
				}
			}
		}
		
		private function onCloseHandler(evt:LibraEvent):void {
			tryToClose();
		}
		
		private function onMouseDownHandler(e:MouseEvent):void {
			if (e.currentTarget == this) {
				dispatchEvent(new Event(CLICKED));
			}
		}
		
		public function setDefaultButton(b:JButton):void {
			if (this.hasComponent(b)) {
				this.defaultButton = b;
			}
		}
		
		public function getDefaultButton():JButton {
			return this.defaultButton;
		}
		
		override public function setActiveable(b:Boolean):void {
			super.setActiveable(b);
			LibraManager.getInstance().addPanel(this);
		}
		
		override public function setSize(size:IntDimension):void {
			super.setSize(size);
			this.closeBtn.setLocation(new IntPoint(size.getWidth() - closeBtn.getWidth()));
		}
		
		public function isModal():Boolean {
			return this.modal;
		}
		
		public function setModal(b:Boolean):void {
			if (this.modal != b) {
				this.modal = b;
			}
		}
		
		public function isCloseable():Boolean {
			return this.closeable;
		}
		
		public function setCloseable(b:Boolean):void {
			if (this.closeable != b) {
				this.closeable = b;
				if (b) {
					if (showing) {
						this.append(this.closeBtn);
						this.closeBtn.addActionListener(onCloseHandler);
					}
				}else {
					if (showing) {
						this.closeBtn.removeActionListener(onCloseHandler);
					}
					this.remove(this.closeBtn);
				}
			}
		}
		
		public function setCloseBtnState(state:IButtonState):void {
			closeBtn.setOpaque(false);
			closeBtn.setText("");
			this.closeBtn.setState(state);
		}
		
		public function setCloseBtnLocation(value:IntPoint):void {
			this.closeBtn.setLocation(value);
		}
		
		override public function toString():String {
			return "JPanel";
		}
		
	}

}